<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\common\Ems;
use app\model\Config;
use app\model\Movie;
use app\model\Tv;
use app\model\User;
use app\tool\Random;
use thans\jwt\facade\JWTAuth;
use think\db\Query;

/**
 * Created by PhpStorm.
 * User: 阿沁
 * Date: 2024/4/12
 * Time: 上午11:33
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是阿沁,健康撸代码,拒绝通宵。
 * 版权所有: © 2024-2030 阿沁
 * WARNING: 未经本人许可, 禁止一切商
 */
class PlatformController extends BaseController
{
	function register()
	{
		$role = $this->request->get('role/d');# 1个人注册  2企业注册  3商家注册
		$email = $this->request->get('email');
		$captcha = $this->request->get('captcha');
		$password = $this->request->get('password');
		$invitecode = $this->request->get('invitecode');
		if (User::getByEmail($email)){
			$this->writeJson(0,'邮箱已存在');
		}
		$parent = User::getByInvitecode($invitecode);

        try {
            Ems::check($email, $captcha, 'register');
        }catch (\Throwable $e){
            $this->writeJson(0, $e->getMessage());
        }


		$salt = Random::alnum();
		$password = $this->getEncryptPassword($password, $salt);
		do {
			$nickname = '用户' . rand(1000000, 9999999);
		} while (User::where(['nickname' => $nickname])->find());
		do {
			$invitecode = Random::alnum();
		} while (User::where(['invitecode' => $invitecode])->find());
		$ip = request()->ip();
		$time = time();
		$data = [
			'level' => 1,
			'score' => 0,
			'avatar' => '/assets/img/avatar.png',
			'invitecode' => $invitecode,
			'nickname' => $nickname,
			'jointime' => $time,
			'joinip' => $ip,
			'logintime' => $time,
			'loginip' => $ip,
			'prevtime' => $time,
			'status' => 'normal',
			'type'=>$role,
			'password'=>$password,
			'salt'=>$salt,
			'pid'=>$parent?$parent->id:0
		];
		$user = User::create($data);
		$token = JWTAuth::builder(['user_id' => $user->id]);
		$this->writeJson(1, '注册成功', '', 'json', ['Authorization' => 'Bearer ' . $token]);
	}

    #重置密码
	function changepwd()
	{
		$email = $this->request->post('email');
		$captcha = $this->request->post('captcha');
		$password = $this->request->post('password');

        try {
           Ems::check($email, $captcha, 'changepwd');
        }catch (\Throwable $e){
            $this->writeJson(0,$e->getMessage());
        }

		$user = User::getByEmail($email);
		if (!$user){
			$this->writeJson(0,'用户不存在');
		}
		$salt = Random::alnum();
		$password = $this->getEncryptPassword($password, $salt);
		$user->save(['password'=>$password,'salt'=>$salt]);
		$this->writeJson();
	}

    #登录
	function login()
	{
		$email = $this->request->get('email');
		$captcha = $this->request->get('captcha');
		$user = User::getByEmail($email);
		if (!$user){
			$this->writeJson(0,'请先注册');
		}

        try {
            Ems::check($email, $captcha, 'login');
        }catch (\Throwable $e){
            $this->writeJson(0,$e->getMessage());
        }

		$time = time();
		$ip = request()->ip();
		$user->prevtime = $user->logintime;
		$user->logintime = $time;
		$user->loginip = $ip;
		$user->save();
		$token = JWTAuth::builder(['user_id' => $user->id]);
		$this->writeJson(1, '登录成功', '', 'json', ['Authorization' => 'Bearer ' . $token]);
	}
	
	function getBanner()
	{
		$row = Config::getFieldByName('platform_banner', 'value');
		$row = explode(',',$row);
		$this->writeJson($row);
	}
	
	#上传电影
	function createMovie()
	{
		$name = $this->request->post('name');
		$class_id = $this->request->post('class_id/d');
		$style_ids = $this->request->post('style_ids');
		$content = $this->request->post('content');
		$horizontalimage = $this->request->post('horizontalimage');
		$verticalimage = $this->request->post('verticalimage');
		$crew = $this->request->post('crew/a'); //[{"crew_id":1,"role":"李云龙"}]
		$solely = $this->request->post('solely/d');#独家合作:1=是,2=否
		$solelyimages = $this->request->post('solelyimages');
		$copyrightimages = $this->request->post('copyrightimages');
		$portraitimages = $this->request->post('portraitimages');
		$involveimages = $this->request->post('involveimages');
		$heraldfile = $this->request->post('heraldfile');
		$videofile = $this->request->post('videofile');
		$length = $this->request->post('length');
		$ret = Movie::create([
			'user_id'=>$this->user_id,
			'name'=>$name,
			'class_id'=>$class_id,
			'style_ids'=>$style_ids,
			'content'=>$content,
			'horizontalimage'=>$horizontalimage,
			'verticalimage'=>$verticalimage,
			'solely'=>$solely,
			'solelyimages'=>$solelyimages,
			'copyrightimages'=>$copyrightimages,
			'portraitimages'=>$portraitimages,
			'involveimages'=>$involveimages,
			'heraldfile'=>$heraldfile,
			'videofile'=>$videofile,
			'length'=>$length,
		]);
		$ret->crew()->saveAll($crew);
		$this->writeJson();
	}
	
	#上传剧集
	function createTv()
	{
		$name = $this->request->post('name');
		$style_ids = $this->request->post('style_ids');
		$num = $this->request->post('num');
		$content = $this->request->post('content');
		$horizontalimage = $this->request->post('horizontalimage');
		$verticalimage = $this->request->post('verticalimage');
		$crew = $this->request->post('crew/a'); //[{"crew_id":1,"role":"李云龙"}]
		$solely = $this->request->post('solely/d');#独家合作:1=是,2=否
		$solelyimages = $this->request->post('solelyimages');
		$copyrightimages = $this->request->post('copyrightimages');
		$portraitimages = $this->request->post('portraitimages');
		$involveimages = $this->request->post('involveimages');
		$files = $this->request->post('files/a'); //[{"title":"xxxx","url":"xxxxx"}]
		if (count($files) != $num){
			$this->writeJson(0,'请上传相对应集数的视频');
		}
		$ret = Tv::create([
			'user_id'=>$this->user_id,
			'name'=>$name,
			'style_ids'=>$style_ids,
			'num'=>$num,
			'content'=>$content,
			'horizontalimage'=>$horizontalimage,
			'verticalimage'=>$verticalimage,
			'solely'=>$solely,
			'solelyimages'=>$solelyimages,
			'copyrightimages'=>$copyrightimages,
			'portraitimages'=>$portraitimages,
			'involveimages'=>$involveimages,
		]);
		$ret->crew()->saveAll($crew);
		$ret->files()->saveAll($files);
		$this->writeJson();
		
	}
	
	#获取电影列表
	function getMovieList()
	{
		$status = $this->request->get('status/d'); #状态:1=待审核,2=待签署合同,3=驳回,4=等待排期,5=等待上线,6=上线
		$rows = Movie::where(['user_id'=>$this->user_id])
			->when(!empty($status),function (Query $query)use($status){
				$query->where('status',$status);
			})
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	#获取剧集列表
	function getTvList()
	{
		$status = $this->request->get('status/d'); #状态:1=待审核,2=待签署合同,3=驳回,4=等待排期,5=等待上线,6=上线
		$rows = Tv::where(['user_id'=>$this->user_id])
			->when(!empty($status),function (Query $query)use($status){
				$query->where('status',$status);
			})
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	#获取电影详情
	function getMovieDetail()
	{
		$movie_id = $this->request->get('movie_id/d');
		$row = Movie::findOrFail($movie_id);
		$this->writeJson($row);
	}
	
	#获取剧集详情
	function getTvDetail()
	{
		$tv_id = $this->request->get('tv_id/d');
		$row = Tv::findOrFail($tv_id);
		$this->writeJson($row);
	}
	
	#修改电影
	function editMovie()
	{
		$params = $this->request->post();
		$row = Movie::findOrFail($params['id']);
		if ($row->status != 3){
			$this->writeJson(0,'状态异常');
		}
		$row->save($params);
		$this->writeJson();
	}
	
	#修改剧集
	function editTv()
	{
		$params = $this->request->post();
		$row = Tv::findOrFail($params['id']);
		if ($row->status != 3){
			$this->writeJson(0,'状态异常');
		}
		$row->save($params);
		$this->writeJson();
	}
	
	#上传电影合同
	function uploadMovieContract()
	{
		$contract = $this->request->post('contract');
		$movie_id = $this->request->post('movie_id/d');
		$row = Movie::findOrFail($movie_id);
		if ($row->status != 2){
			$this->writeJson(0,'状态异常');
		}
		$row->contract = $contract;
		$row->status = 4;
		$row->save();
		$this->writeJson();
	}
	
	#上传剧集合同
	function uploadTvContract()
	{
		$contract = $this->request->post('contract');
		$tv_id = $this->request->post('tv_id/d');
		$row = Tv::findOrFail($tv_id);
		if ($row->status != 2){
			$this->writeJson(0,'状态异常');
		}
		$row->contract = $contract;
		$row->status = 4;
		$row->save();
		$this->writeJson();
	}
	
	#电影数据管理
	function getMovieDataList()
	{
		$rows = Movie::order(['id'=>'desc'])->paginate()->items();
		$this->writeJson($rows);
	}
	
	#剧集数据管理
	function getTvDataList()
	{
		$rows = Tv::order(['id'=>'desc'])->paginate()->items();
		$this->writeJson($rows);
	}
	


	

}
